import Taro, {Component, Config} from '@tarojs/taro'
import {View, Image, Text, Button} from '@tarojs/components'
import hintIcon from '../../common/assets/popup_icon@2x.png';
import ajax from "../../net/ajax";
import apiObject from "../../net/api";
import common from "../../common/js/common";
import Dialog from "../../common/components/dialog";
import './loginDialog.scss'

type Props = {
    onCloseLoginDialog: any,
    onRefreshInfo: Function
    onSetOpenType: any,
    openType: string
}

class LoginDialog extends Component<Props, {}> {

    cancel(type = 'check') {
        this.props.onCloseLoginDialog && this.props.onCloseLoginDialog(type)
    }

  async getPhoneNumberCallBack(e) {
    console.log("getPhoneNumberCallBack==>",e)
    if(e.detail.encryptedData && e.detail.iv){
      Taro.showLoading({title: '登录中...'});
      let res = await Taro.login();
      if (res.errMsg == 'login:ok') {
        console.log("res===>",res)
        await this.getPhoneNumber(res.code, e.detail.encryptedData, e.detail.iv);
      } else {
        Taro.showToast({
          title: '登录失败,请重试!',
          icon: 'none',
          duration: 2000
        })
      }
    }
    this.cancel('check');
  }
  getUserInfo2(){
    wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
            console.log("res.userInfo===>",res, res.userInfo,)
            common.setUserInfo(res.userInfo);
            this.cancel();
        }
      })
  }
  getUserInfoCallBack(e){
    wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
            console.log("res.userInfo===>",res, res.userInfo,)
        //   this.setData({
        //     userInfo: res.userInfo,
        //     hasUserInfo: true
        //   })
        }
      })
    this.cancel();
  }

  async getPhoneNumber(wxCode, encryptedData, iv) {
      let params = {
          code: wxCode,
          encrypdata: encryptedData,
          ivdata: iv
      };
      let res = await ajax.post(apiObject.getUserPhone, params);
      if (res.statusCode == 200) {
          common.setPhoneNumber(res.data.phoneNumber);
      } else {
          //请求异常
          Taro.showToast({
              title: '请求异常,请重试!',
              icon: 'none',
              duration: 2000
          })
      }
      await Taro.getUserInfo().then(() => {
          common.getUserInfo().then(() => {
              this.cancel()
          });
      }).catch(() => {
          this.openGetUserInfo();
      })
  }

    openGetUserInfo() {
        Taro.hideLoading();
        this.props.onSetOpenType && this.props.onSetOpenType()
    }


    render() {
        const {openType} = this.props;
        return (
            <Dialog>
                <View className='invite-content' catchtouchmove='true'>
                    <View className='invite-card-content'>
                        <Image
                            className='hint-img'
                            src={hintIcon}
                            mode='widthFix'
                        />
                        <Text className='hint-title'>{openType === 'getPhoneNumber'?'获取手机号':'获取用户信息'}</Text>
                        {openType == 'getPhoneNumber' &&
                        <Button className='opacityBtn' open-type='getPhoneNumber'
                                onGetPhoneNumber={this.getPhoneNumberCallBack.bind(this)}>
                            <Text className='hint-btn' onClick={this.cancel.bind(this)}>一键登录/注册抱果下山</Text>
                        </Button>
                        }
                        {openType == 'getUserInfo' &&
                        <Button className='opacityBtn'
                                onClick={this.getUserInfo2.bind(this)} 
                                // onGetUserInfo={this.getUserInfoCallBack.bind(this)}
                                >
                            <Text className='hint-btn'>获取用户信息授权</Text>
                        </Button>
                        }
                        <Text className='hint-no' onClick={this.cancel.bind(this,'nocheck')}>取消</Text>
                    </View>
                </View>
            </Dialog>
        )
    }
}


export default LoginDialog
